package smartq.app;

import java.sql.ResultSet;

import smartq.DB.Connect;

import com.app.smart_q.R;



import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SmartQ_Login_Activity extends Activity{
	
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressDialog mProgressDialog;
	private Boolean Data = false;
	
	Connect con = new Connect(); 
	ResultSet rs=null;
	String   CustomerFirstName
			,CustomerLastName
			,Customersex
			,CustomerClass
			,CustomerPhonenumber
			,PhonenumberCh;
	
	EditText getPhonenumber;
	String Phonenumber;
	Intent newActivity;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		
	//-----------------------------Button_OK--------------------------------//		
		final Button OK = (Button) findViewById(R.id.button_activity_login_buttonlogin);
		OK.setOnClickListener(new View.OnClickListener() {
			
			
			
			@Override
			public void onClick(View v) {
				new  Loading().execute();
				
				
				// TODO Auto-generated method stub
				
			}
		
		});

	}
	
	 @Override
	    protected Dialog onCreateDialog(int id) {
	        switch (id) {
	        case DIALOG_DOWNLOAD_PROGRESS:
	            mProgressDialog = new ProgressDialog(this);
	            mProgressDialog.setMessage("Loading.....");
	            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	            mProgressDialog.setCancelable(true);
	            mProgressDialog.show();
	            return mProgressDialog;
	        default:
	            return null;
	        }
	    }
	 	//-----------AsyncTask-Loading----------------------------------//
	    public class Loading extends AsyncTask<String, Void, Void> {
	        @SuppressWarnings("deprecation")
			protected void onPreExecute() {
	        	super.onPreExecute();
	        	showDialog(DIALOG_DOWNLOAD_PROGRESS);
	        	getPhonenumber = (EditText) findViewById(R.id.Edittext_activity_login_login);
	        	Phonenumber = getPhonenumber.getText().toString();
				PhonenumberCh = Phonenumber;
	        }
	        @Override
	        protected Void doInBackground(String... params) {
	            // TODO Auto-generated method stub
	        	try{
					String StrSql = "SELECT * FROM Customers WHERE CustomerPhonenumber ='"+Phonenumber+"' " +
						"AND IsDeleted= '0' ";
					rs = con.selectData(StrSql);
				}catch (Exception e) {
					Toast.makeText(SmartQ_Login_Activity.this,e.toString() ,
					Toast.LENGTH_LONG).show(); 
				}	
				try {
					if (rs.next()) {
						Data = true;
						CustomerPhonenumber = rs.getString("CustomerPhonenumber");
						CustomerFirstName = rs.getString("CustomerFirstName");
						CustomerLastName = rs.getString("CustomerLastName");
						Customersex = rs.getString("Customersex");
						CustomerClass = rs.getString("CustomerClass");
						
						newActivity = new Intent(SmartQ_Login_Activity.this,SmartQ_Tap_Main.class);
//						// input = true;
						newActivity.putExtra("CustomerPhonenumber", CustomerPhonenumber);
						newActivity.putExtra("CustomerFirstName", CustomerFirstName);
						newActivity.putExtra("CustomerLastName", CustomerLastName);
						newActivity.putExtra("Customersex", Customersex);
						newActivity.putExtra("CustomerClass", CustomerClass);
							
//							Toast.makeText(LoginActivity.this,"Well Come : "+CustomerFirstName+' '+CustomerLastName ,
//									Toast.LENGTH_SHORT).show(); 
					}else if(!Phonenumber.equals("") && Phonenumber.length()==11  ){
						Data = true;
						newActivity = new Intent(SmartQ_Login_Activity.this,SmartQ_Tap_Main.class);

						newActivity.putExtra("CustomerPhonenumber", Phonenumber);
						newActivity.putExtra("CustomerFirstName", "");
						newActivity.putExtra("CustomerLastName", "");
						newActivity.putExtra("Customersex", "");
						newActivity.putExtra("CustomerClass", "");

						
					}
				}catch (Exception e) {
						Toast.makeText(SmartQ_Login_Activity.this,e.toString() ,
						Toast.LENGTH_LONG).show(); 
				}
				
	    		return null;
	        }

	        @SuppressWarnings("deprecation")
			protected void onPostExecute(Void unused) {
	            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
	            removeDialog(DIALOG_DOWNLOAD_PROGRESS);
	            if(Data){
	            	if(Adddata()){
						startActivity(newActivity);
						finish();
					}else{
						Toast.makeText(SmartQ_Login_Activity.this,"Add Data Fail!!!",Toast.LENGTH_SHORT).show(); 
						startActivity(newActivity);
						finish();
					}
	        	}else{
					Toast.makeText(SmartQ_Login_Activity.this,"หมายเลขโทรศัพท์ไม่ถูกต้อง\n กรุณาตรวจสอบอีกครั้ง",Toast.LENGTH_SHORT).show(); 
				}

	            
	        	
	        }
	        
	    }
	    
	public Boolean Adddata(){
		final myDBClass myDb = new myDBClass(this);
		
		long saveStatus = myDb.InsertData("1",
				PhonenumberCh,
				PhonenumberCh);
    	if(saveStatus <=  0)
    	{
            return false;
    	}
		return true;
	}
	
}
